package com.kipling.kio.server;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.kipling.kio.core.GroupContext;
import com.kipling.kio.core.intf.KioHandler;
import com.kipling.kio.core.intf.Packet;
import com.kipling.kio.core.stat.GroupStat;
import com.kipling.kio.core.task.DecodeRunnable;
import com.kipling.kio.server.intf.ServerKioHandler;
import com.kipling.kio.server.threadpool.DefaultThreadFactory;

/** 
 * @author KiplingLee E-mail: kiplinglee@163.com
 * @version 创建时间：2017年5月3日 上午10:14:51 
 * 
 */
public class ServerGroupContext< P extends Packet, R> extends GroupContext< P, R> {
	
	static Logger log = LoggerFactory.getLogger(ServerGroupContext.class);
    
	protected ServerGroupStat serverGroupStat = new ServerGroupStat();
	
	private ServerKioHandler< P, R> serverKioHandler = null;
	
	private DecodeRunnable<P, R> decodeRunnable = null;

	/** The group executor. */
	protected ExecutorService groupExecutor = null;

	private ThreadPoolExecutor closePoolExecutor = null;
	
	private SocketChannelHandle<P, R> socketChannelHandle = null;
	
	public ServerGroupContext(ServerKioHandler< P, R> serverKioHandler){
		this(serverKioHandler, new ThreadPoolExecutor(CORE_POOL_SIZE, CORE_POOL_SIZE, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(),
				DefaultThreadFactory.getInstance("t-aio-server-group")));
	}
	
	public ServerGroupContext(ServerKioHandler< P, R> serverKioHandler,ThreadPoolExecutor groupExecutor){
		super();
		this.groupExecutor = groupExecutor;
		this.decodeRunnable = new DecodeRunnable< P, R>(this);		
		this.setServerKioHandler(serverKioHandler);
		this.socketChannelHandle = new SocketChannelHandle< P, R>(this);	
	}
	
	@Override
	public KioHandler<P, R> getKioHandler() {
		// TODO Auto-generated method stub
		return serverKioHandler;
	}

	public ServerGroupStat getServerGroupStat() {
		return serverGroupStat;
	}

	public void setServerGroupStat(ServerGroupStat serverGroupStat) {
		this.serverGroupStat = serverGroupStat;
	}

	public ThreadPoolExecutor getClosePoolExecutor() {
		return closePoolExecutor;
	}

	public void setClosePoolExecutor(ThreadPoolExecutor closePoolExecutor) {
		this.closePoolExecutor = closePoolExecutor;
	}

	@Override
	public GroupStat getGroupStat() {
		// TODO Auto-generated method stub
		return this.getServerGroupStat();
	}
	/**
	 * @return the serverAioHandler
	 */
	public ServerKioHandler< P, R> getServerKioHandler()
	{
		return serverKioHandler;
	}

	/**
	 * @param serverAioHandler the serverAioHandler to set
	 */
	public void setServerKioHandler(ServerKioHandler< P, R> serverKioHandler)
	{
		this.serverKioHandler = serverKioHandler;
	}
	/**
	 * @return the groupExecutor
	 */
	@Override
	public ExecutorService getGroupExecutor()
	{
		return groupExecutor;
	}

	/**
	 * @param groupExecutor the groupExecutor to set
	 */
	@Override
	public void setGroupExecutor(ExecutorService groupExecutor)
	{
		this.groupExecutor = groupExecutor;
	}

	public DecodeRunnable<P, R> getDecodeRunnable() {
		return decodeRunnable;
	}

	public void setDecodeRunnable(DecodeRunnable<P, R> decodeRunnable) {
		this.decodeRunnable = decodeRunnable;
	}

	public SocketChannelHandle<P, R> getSocketChannelHandle() {
		return socketChannelHandle;
	}

	public void setSocketChannelHandle(SocketChannelHandle<P, R> socketChannelHandle) {
		this.socketChannelHandle = socketChannelHandle;
	}

}
